<div class="portlet box grey">
    <div class="portlet-title">
        <div class="tools">
            <a href="javascript:;" class="collapse"></a>
        </div>
        <form method="get" class="form-horizontal">
            <div class="control-group float-left">
                <div id="date_range" class="btn">
                    <i class="icon-calendar"></i>&nbsp;<span></span>
                    <b class="caret"></b>
                    <input type="hidden" name="dateBegin" id="date_begin" value="<?php echo $dateBegin ?>"/>
                    <input type="hidden" name="dateEnd" id="date_end" value="<?php echo $dateEnd ?>"/>
                </div>
            </div>

            <div class="control-group float-left">
                <select id="store_id" name="store_id" class="m-wrap">
                    <option value="0">[Салбар]</option>
                    <?php foreach ($stores as $i => $v): ?>
                        <option value="<?php echo $i ?>" <?php echo $i == $storeId ? 'selected="selected"' : ''; ?>><?php echo $v ?></option>
                    <?php endforeach; ?>
                </select>
            </div>

            <div class="control-group float-left">
                <select id="category_id" name="category_id" class="m-wrap">
                    <option value="0">[Ангилал]</option>
                    <?php foreach ($categories as $i => $v): ?>
                        <option value="<?php echo $i ?>" <?php echo $i == $categoryId ? 'selected="selected"' : ''; ?>><?php echo $v ?></option>
                    <?php endforeach; ?>
                </select>
            </div>

            <div class="control-group float-left">
                <select id="product_id" name="product_id" class="m-wrap">
                    <option value="0">[Бараа]</option>
                </select>
            </div>

            <div class="control-group float-left">
                <button type="submit" class="btn btn-primary">
                    <i class="icon-filter icon-white"></i> Шүүх
                </button>
            </div>
        </form>
    </div>
    <div class="portlet-body">
        <div class="widget-box">
            <table id="inventory_items" class="table table-striped table-bordered table-hover" cellspacing="0" width="100%">
                <thead>
                    <tr>
                        <th></th>
                        <th>Огноо</th>
                        <th>Салбар</th>
                        <th>Ангилал</th>
                        <th>Бараа</th>
                        <th>Эхний үлдэгдэл</th>
                        <th>Эцсийн үлдэгдэл</th>
                        <th>Орлого</th>
                        <th>Зарлага</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if ($rows): ?>
                        <?php $i = 0; ?>
                        <?php foreach ($rows AS $row): ?>
                            <tr>
                                <td><?php echo ++$i ?></td>
                                <td><?php echo $row['inv_date'] ?></td>
                                <td><?php echo $row['store_name'] ?></td>
                                <td><?php echo $row['category_name'] ?></td>
                                <td><?php echo $row['product_name'] ?></td>
                                <td><?php echo $row['qty_first'] ?></td>
                                <td><?php echo $row['qty_last'] ?></td>
                                <td><?php echo $row['qty_in'] ?></td>
                                <td><?php echo $row['qty_out'] ?></td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<script>
    function categoryChanged() {
        c = $(this).val();
        if (c > 0) {
            $.ajax({
                url: '<?php echo $urlProduct ?>?category_id=' + c,
                dataType: 'json',
                success: function(json) {
                    if ($.isEmptyObject(json)) {
                        $('#product_id').html('<option value="0">[Ангилалд бараа алга]</option>');
                    } else {
                        $('#product_id').html('');
                        $('#product_id').html('<option value="0">[Бүгд]</option>');
                        $.each(json, function(i, v) {
                            $('#product_id').append('<option value="' + i + '">' + v + '</option>');
                        });
                    }
                }
            });
        } else {
            $('#product_id').html('<option value="0">[Бараа]</option>');
        }
    }

    $(function() {

        dateBegin = '<?php echo $dateBegin ?>';
        dateEnd = '<?php echo $dateEnd ?>';

        $('#date_range').daterangepicker({
            ranges: {
                'Өнөөдөр': ['today', 'today'],
                'Өчигдөр': ['yesterday', 'yesterday'],
                'Сүүлийн 7 хоног': [Date.today().add({
                        days: -6
                    }), 'today'],
                'Сүүлийн 30 хоног': [Date.today().add({
                        days: -30
                    }), 'today'],
                'Энэ сар': [Date.today().moveToFirstDayOfMonth(), Date.today().moveToLastDayOfMonth()],
                'Өнгөрсөн сар': [Date.today().moveToFirstDayOfMonth().add({
                        months: -1
                    }), Date.today().moveToFirstDayOfMonth().add({
                        days: -1
                    })]
            },
            opens: (App.isRTL() ? 'left' : 'right'),
            format: 'yyyy-MM-dd',
            separator: ' - ',
            startDate: dateBegin,
            endDate: dateEnd,
            minDate: '01/01/2012',
            maxDate: '12/31/2014',
            locale: {
                applyLabel: 'Сонгох',
                fromLabel: 'эхний',
                toLabel: 'сүүлийн',
                customRangeLabel: 'Өөрөө сонгох',
                daysOfWeek: ['Бү', 'Да', 'Мя', 'Лх', 'Пү', 'Ба', 'Ха'],
                monthNames: ['1-р сар', '2-р сар', '3-р сар', '4-р сар', '5-р сар', '6-р сар', '7-р сар', '8-р сар', '9-р сар', '10-р сар', '11-р сар', '12-р сар'],
                firstDay: 1
            },
            showWeekNumbers: true,
            buttonClasses: ['btn-danger']
        },
        function(start, end) {
            $('#date_range span').html(start.toString('yyyy-MM-dd') + ' - ' + end.toString('yyyy-MM-dd'));
            $('#date_begin').val(start.toString('yyyy-MM-dd'));
            $('#date_end').val(end.toString('yyyy-MM-dd'));
        });

        $('#date_range span').html(dateBegin + ' - ' + dateEnd);


        $('#inventory_items').DataTable({
            'paging': false,
            "language": {
                "emptyTable": "Хүснэгт хоосон байна.",
                "info": "_START_-с _END_ хүртэл, Нийт: _TOTAL_",
                "infoEmpty": "Нийт: 0",
                "infoFiltered": "(_MAX_ бичлэгээс хайсан)",
                "infoPostFix": "",
                "infoThousands": ",",
                "lengthMenu": "_MENU_ мөр харах",
                "loadingRecords": "Уншиж байна...",
                "processing": "Тооцоолж байна...",
                "search": "Хайлт:",
                "zeroRecords": "Хайлтын үр дүн олдсонгүй",
                "paginate": {
                    "first": "Эхнийх",
                    "last": "Сүүлийнх",
                    "next": "Дараагийнх",
                    "previous": "Өмнөх"
                },
                "aria": {
                    "sortAscending": ": Өсөхөөр эрэмблэх",
                    "sortDescending": ": Буурахаар эрэмблэх"
                }
            }
        });
        $('#category_id').change(categoryChanged);
    });
</script>